/*
 * app_usound_button.c
 *
 *  Created on: 17 Aug 2021
 *      Author: NingTao.Huang
 */
#include "driver_Mobvoi_includes.h"
#include "app_Mobvoi_includes.h"
#include "Mobvoi_external.h"
#include "bt_at_types.h"
#include "app_button.h"

#if (CONFIG_USE_USB == 1)
static int app_button_volp_action(void)
{
    app_handle_t app_h = app_get_sys_handler();

    LOG_I(BTN, "volp\r\n");

    app_h->usound_vol++;

    /*   if (!app_bt_flag2_get(AP_FLAG2_LINEIN_MUTE))
    {
        aud_volume_mute(0);
		aud_PAmute_operation(0);
    }*/

    if (app_h->usound_vol > AUDIO_VOLUME_MAX)
    {
        app_h->usound_vol = AUDIO_VOLUME_MAX;
    }

    if (!app_wave_playing())
        aud_dac_set_volume(app_h->usound_vol);

    if (app_h->usound_vol == AUDIO_VOLUME_MAX)
        app_wave_file_play_start(APP_WAVE_FILE_ID_VOL_MAX);

    return 0;
}

static int app_button_volm_action(void)
{
    app_handle_t app_h = app_get_sys_handler();

    LOG_I(BTN, "volm\r\n");

    app_h->usound_vol--;

    if (app_h->usound_vol <= AUDIO_VOLUME_MIN)
    {
        app_h->usound_vol = AUDIO_VOLUME_MIN;
        if (!app_wave_playing())
        {
            aud_volume_mute(1);
            aud_PAmute_operation(1);
        }
    }

    if (!app_wave_playing())
        aud_dac_set_volume(app_h->usound_vol);

    if (app_h->usound_vol == AUDIO_VOLUME_MIN)
        app_wave_file_play_start(APP_WAVE_FILE_ID_VOL_MIN);

    return 0;
}

static int app_button_playpause_action(void)
{
    return 0;
}

static int app_button_vol_mute_action(void)
{
    LOG_I(BTN, "vol_mute\r\n");

#if (CONFIG_CUSTOMER_BUTTON_HID_MODE == 1)
    app_customer_hid_switch_mode();
#else
    app_handle_t app_h = app_get_sys_handler();

    if (app_h->volume_store & 0x40)
    {
        app_h->volume_store &= ~0x40;
        aud_volume_mute(0);
    }
    else
    {
        app_h->volume_store |= 0x40;
        app_wave_file_play_stop();
        aud_volume_mute(1);
    }
#endif

    return 0;
}

CONST button_action_func usb_audio_button_handler[BUTTON_BT_END] =
    {
        NULL,
        app_button_playpause_action, //app_button_playpause_action,
        NULL,
        NULL,
        NULL,
        NULL,
        app_button_volp_action,     //app_button_volp_action,
        app_button_volm_action,     //app_button_volm_action,
        app_button_vol_mute_action, //app_button_vol_mute_action,
        NULL,
        app_button_powerdown,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL, //app_button_clear_memory,
        NULL, //app_button_enter_dut_mode,
        NULL, //app_button_lang_change,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL, //BUTTON_STEREO,
        NULL, //BUTTON_RESERVED1,
        NULL, //BUTTON_RESERVED2,
        NULL, //BUTTON_RESERVED3,
};

#endif
